<template>
	<view>
		<view style="background-color: #FFFFFF;padding-bottom: 10px;">
			<!-- <view style="width: 95%;margin: 0 auto;">
				<uni-search-bar @confirm="search" v-model="title" placeholder="搜索岗位"></uni-search-bar>
			</view> -->
			<uni-swiper-dot :info="bannerList" :current="current" field="content" mode="round" :dots-styles="dotStyle">
				<swiper @change="bannerCahnge" autoplay style="width: 90%;margin: 0 auto;height: 150px;border-radius: 10px;">
					<swiper-item v-for="(item,index) in bannerList" :key="index">
						<image @click="toPost(item.id)" :src="path+strToArr(item.image)[0]" mode="aspectFill" style="width: 100%;height: 150px;border-radius: 10px;"></image>
					</swiper-item>
				</swiper>
			</uni-swiper-dot>
			<view style="width: 90%;margin: 15px auto 0px auto;display: flex;text-align: center;">
				<view @click="bindType(0)" style="width: 50%;">
					<image src="@/static/image/zhengshi.png" mode="aspectFit" style="width: 100%;height: 40px;"></image>
				</view>
				<view @click="bindType(1)" style="width: 50%;">
					<image src="@/static/image/paiqian.png" mode="aspectFit" style="width: 100%;height: 40px;"></image>
				</view>
			</view>
		</view>
		
		<view style="width: 100%;display: flex;">
			<view style="width: 20%;">
				<view @click="bindCityIndex(index)" v-for="(item,index) in cityList" :key="index" class="city shenglue" :class="cityIndex==index?'city_1':''"
				:style="{borderRadius: index==cityList.length-1?'0 0 8px 8px':''}">{{item.city}}({{item.num}})</view>
			</view>
			<view style="width: 80%;">
				<view  @click="toPost(item.id)" v-for="(item,index) in postList" :key="index" class="kuang">
					<view style="display: flex;justify-content: space-between;">
						<view style="width: 36%;">
							<image :src="path+strToArr(item.image)[0]" mode="aspectFill" style="width: 100%;height: 100px;border-radius: 4px;"></image>
						</view>
						<view style="width: 60%;">
							<view style="font-size: 16px;height: 30px;">{{item.title}}</view>
							<view style="font-size: 14px;color: red;height: 20px;line-height: 20px;font-weight: 700;">{{item.salary}}</view>
							<view style="width: 100%;white-space: nowrap;">
								<view v-for="(label,i) in strToArr(item.welfare)" :key="i" v-if="i<3" class="label">{{label}}</view>
							</view>
							<view style="font-size: 14px;color: #999999;height: 20px;line-height: 25px;">{{item.type==0?'正式工':'派遣工'}}</view>
						</view>
					</view>
					<view v-if="item.userNum > 0" style="width: 100%;display: flex;height: 20px;position: relative;">
						<view style="width: 50%;color: #1C8162;font-size: 12px;line-height: 25px;">{{item.applyNum}}人已报名</view>
						<view style="width: 50%;text-align: right;">
							<!-- <view class="label" style="background-color: #1C8162;color: #FFFFFF;position: absolute;top: 0px;right: 0;margin-right: 0;">未报名</view> -->
							<view class="label" style="background-color: #1C8162;color: #FFFFFF;position: absolute;top: 0px;right: 0;margin-right: 0;">已报名</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view style="width: 100%;height: 50px;"></view>
	</view>
</template>

<script>
	import api from '@/static/js/request.js'
	import data from '@/static/js/util.js'
	export default {
		data() {
			return {
				path: api.getPath(),
				title: '',
				
				current: 0,
				cityIndex: 0,
				cityList: [
					{city: '全部', num: 0}
				],
				postList: [],
				bannerList: [],
				
				dotStyle: {
					backgroundColor: 'rgba(255,255,255,0.3)',
					border: '1px rgba(255,255,255,0.3) solid',
					color: '#fff',
					selectedBackgroundColor: 'rgba(255,255,255,0.9)',
					selectedBorder: '1px rgba(255,255,255,0.9) solid'
				},
				
				type: -1,
				pageNo: 1,
				pageSize: 10,
				total: 0,
			}
		},
		onLoad() {
			let _this = this;
			let loginData = null;
			try {
				uni.login({
					success(loginData) {
						api.login({
							code: loginData.code,
							anonymousCode: loginData.anonymousCode,
							state: 0
						}).then((res)=>{
							uni.setStorageSync('user',res.data);
						}).finally(()=>{
							_this.getPostGroupCity();
							_this.getPostList();
						})
					}
				})
			} catch (err) {
				uni.showToast({
					title: '用户取消登录抖音',
					icon: 'none',
				})
			}
			
		},
		onReachBottom() {
			if(this.total > this.pageNo * this.pageSize){
				this.pageNo ++;
				this.getPostPage()
			}
		},
		methods: {
			bindType(type){
				if(type == this.type){
					this.type = -1;
				}else{
					this.type = type;
				}
				this.pageNo = 1;
				this.getPostList();
				this.getPostGroupCity();
			},
			strToArr(str){
				return data.strToArr(str);
			},
			bannerCahnge(e){
				this.current = e.detail.current;
			},
			search(e){
				this.title = e.value;
				this.pageNo = 1;
				this.getPostList();
				this.getPostGroupCity();
			},
			bindCityIndex(index){
				this.cityIndex = index;
				this.pageNo = 1;
				this.getPostList();
				this.getPostGroupCity();
			},
			getPostGroupCity(){
				let _this = this;
				api.getPostGroupCity({
					title: this.title,
					type: this.type,
				}).then((res)=>{
					let obj = {city: '全部', num: 0};
					res.data.forEach((item)=>{
						obj.num = data.addNum(obj.num,item.num)
					})
					_this.cityList = [obj];
					_this.cityList.push(...res.data);
				})
			},
			getPostList(){
				let _this = this;
				let userId = 0;
				let user = uni.getStorageSync('user');
				if(user != undefined && Object.keys(user).length > 0){
					userId = user.id;
				}
				api.getPostList({
					title: this.title,
					userId: userId,
					type: this.type,
					city: this.cityList[this.cityIndex].city,
					pageNo: this.pageNo,
					pageSize: this.pageSize,
				}).then((res)=>{
					_this.bannerList = res.data.records;
					_this.postList = res.data.records;
					_this.total = res.data.total;
				})
			},
			getPostPage(){
				let _this = this;
				let userId = 0;
				let user = uni.getStorageSync('user');
				if(user != undefined && Object.keys(user).length > 0){
					userId = user.id;
				}
				api.getPostList({
					title: this.title,
					userId: userId,
					type: this.type,
					pageNo: this.pageNo,
					pageSize: this.pageSize,
				}).then((res)=>{
					_this.bannerList.push(...res.data.records);
					_this.postList.push(...res.data.records);
					_this.total = res.data.total;
				})
			},
			toPost(postId){
				uni.navigateTo({
					url: '/pages/post/post?postId='+postId
				})
			},
		}
	}
</script>

<style>
	.label{
		padding: 0 5px;
		background-color: #EDF0F2;
		height: 20px;
		line-height: 20px;
		border-radius: 4px;
		font-size: 12px;
		display: inline-block;
		margin-right: 10px;
	}
	.city{
		width: 100%;
		height: 50px;
		line-height: 50px;
		text-align: center;
		background-color: #FFFFFF;
		font-size: 15px;
	}
	.city_1{
		background-color: #EDF0F2;
		color: #1C8162;
		font-weight: 700;
	}
</style>
